<?php

class TestWakFile extends UnitTestCase {

    function setUp() {
        clearstatcache();
        $this->tmp=dirname(__FILE__).'/tmp.txt';
        $this->renamed=dirname(__FILE__).'/renamed.txt';
        $this->movedir=dirname(__FILE__).'/folder/';
        $this->moved=$this->movedir.'renamed.txt';
        $this->_deleteIfExists($this->tmp);
        $this->_deleteIfExists($this->renamed);
        $this->_deleteIfExists($this->moved);
    }

    function tearDown() {
        $this->_deleteIfExists(dirname(__FILE__).'/tmp.txt');
        $this->_deleteIfExists(dirname(__FILE__).'/renamed.txt');
    }

    function testFileCycle() {
        // creating a non-existing file instance
        $file=$this->tmp;
        $tmp=new WakFile($file); // echo $tmp->path;
        $this->assertEqual($tmp->path(),$file
            ,"path() should return path given to constructor");
        // trying read/info functions on a non-existing file
        if (!$this->assertFalse($tmp->exists()
            ,'exists() should return false for non-existing files')
        or !$this->assertFalse($tmp->delete()
            ,'delete() should return false if file dont exist')
        or !$this->assertFalse($tmp->isReadable()
            ,'isReadable() should return false for non-existing files')
        or !$this->assertFalse($tmp->read()
            ,'read() should return false for non-existing files')
        or !$this->assertFalse($tmp->isWritable()
            ,'isWritable() should return false for non-existing files')
        or !$this->assertEqual($tmp->size(),0
            ,'size() should return zero for non-existing files %s')
        or !$this->assertFalse($tmp->move($this->moved)
            ,'move() should return false for non-existing files')
        // checking the file was not created by previous calls
        or !$this->assertFalse(file_exists($file)
            ,'file '.$file.' should still not exist at this point')) {
            $this->dump($tmp);
            return false;
        }
        // creating the file
        if (!$this->assertTrue($tmp->create(),'create() should return true when creating a non-existing file')
        or !$this->assertTrue(file_exists($file),'create() should create the file')) {
            return false;
        }
        $this->assertEqual($tmp->basename(),'tmp.txt'
            ,"basename() should return base name");
        $this->assertEqual($tmp->dirname(),dirname(__FILE__)
            ,"dirname() should return container folder path %s");

        clearstatcache();

        // moving/renaming test
        $this->assertTrue($tmp->rename('renamed.txt'),'rename() should return true');
        $this->assertTrue($tmp->move($this->movedir),'move() should return true');
        $this->assertTrue(file_exists($this->moved)
            ,"... and move the file");

        // print_r($tmp);
        // deleting
        $this->assertTrue($tmp->delete(),'delete() should return true for existing file');
        $this->assertFalse(file_exists($file),'delete() should destroy the file');

        // $this->dump($tmp);
    }

    function testReadWrite() {
        $f=new WakFile($this->tmp);
        $this->assertTrue($f->write('01234')
            ,"write('01234') should return true even if file don't exist");
        $this->assertEqual($f->read(3,1),'123'
            ,"read(3,1) should read 3 bytes after first byte %s");
        $this->assertTrue($f->write('456789')
            ,"write('456789') should return true and write at current position");
        $this->assertTrue($f->toStart()
            ,"toStart() should return true...");
        $this->assertEqual($f->offset(),0
            ,"... and set offset to start. (0)");
        $this->assertEqual($f->read(),'0123456789'
            ,"file content should be '0123456789' at this point");
        $this->assertTrue($f->write('654',4)
            ,"write('654',4) should return true...");
        $this->assertEqual($f->read(3,4),'654'
            ,"...and write '654' after 4th byte.");
        $this->assertEqual($f->read(1024,0),'0123654789'
            ,"therefore, file content should be '0123654789' at this point %s");
        $this->assertTrue($f->erase(4)
            ,"erase(4) should return true (and erase all from 4th byte");
        $this->assertEqual($f->read(1024,0),'0123'
            ,"therefore, file content should be '0123' at this point %s");
    }

    function _deleteIfExists($file) {
        if (file_exists($file) and !unlink($file)) {
            $this->fail("cant' start tests, file '.$file.' should not exist before tests %s'");
            return false;
        }
        return true;
    }
}
